package com.mf.service.impl;


import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect;
import org.springframework.stereotype.Service;

import com.mf.entity.User;
import com.mf.repository.UserRepository;
import com.mf.service.UserService;
import com.mf.util.StringUtil;

/**
 * 用户Service实现类
 *
 * @author Administrator
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public User findByUserName(String userName) {
        return userRepository.findByUserName(userName);
    }

    @Override
    public List<User> list(User user, Integer page, Integer pageSize, Direction direction, String... properties) {
        Pageable pageable = new PageRequest(page - 1, pageSize);
        Page<User> pageUser = userRepository.findAll(new Specification<User>() {

            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (user != null) {
                    if (StringUtil.isNotEmpty(user.getUserName())) {
                        predicate.getExpressions().add(cb.like(root.get("userName"), "%" + user.getUserName() + "%"));
                    }
                    predicate.getExpressions().add(cb.notEqual(root.get("id"), 1));
                }
                return predicate;
            }
        }, pageable);
        return pageUser.getContent();
    }

    @Override
    public Long getCount(User user) {
        Long count = userRepository.count(new Specification<User>() {

            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (user != null) {
                    if (StringUtil.isNotEmpty(user.getUserName())) {
                        predicate.getExpressions().add(cb.like(root.get("userName"), "%" + user.getUserName() + "%"));
                    }
                    predicate.getExpressions().add(cb.notEqual(root.get("id"), 1));
                }
                return predicate;
            }

        });
        return count;
    }

    @Override
    public void save(User user) {

        //新增用户时 或 更新用户密码不为空
        if (user.getId().equals(null) || (user.getId() != null && user.getPassword() != "")) {
            /**
             * 加密password 和 更新password_key
             * 每次修改时 密码与password_key 都会更变；
             *
             */
            String code = StringUtil.randomKey(8);
            String salt = Hex.encodeToString(StringUtil.saltBytes());    //shiro加密库
            String password = new Sha1Hash((user.getPassword() + code).getBytes(), salt, 4096).toString();
            user.setPassword_key(code);
            user.setPassword(password);
        }

        userRepository.save(user);
    }


    @Override
    public void delete(Integer id) {
        userRepository.delete(id);
    }

    @Override
    public User findById(Integer id) {
        return userRepository.findOne(id);
    }


}
